package com.ybwh.springboot1.websocket.client;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class OkHttpClientUtils {
	
	public static void sendSync() {
		OkHttpClient client = new OkHttpClient.Builder().readTimeout(5, TimeUnit.SECONDS).build();
        Request request = new Request.Builder().url("http://www.baidu.com")
                .get().build();
        Call call = client.newCall(request);
        try {
            Response response = call.execute();
            System.out.println(response.body().string());
        } catch (IOException e) {
            e.printStackTrace();
        }
	}
	
	public static void sendAsync() {
		 OkHttpClient client = new OkHttpClient.Builder().readTimeout(5, TimeUnit.SECONDS).build();
	        Request request = new Request.Builder().url("http://www.baidu.com")
	                .get().build();
	        Call call = client.newCall(request);
	        call.enqueue(new Callback() {
	            @Override
	            public void onFailure(Call call, IOException e) {
	                System.out.println("Fail");
	            }

	            @Override
	            public void onResponse(Call call, Response response) throws IOException {

	                System.out.println(response.body().string());

	            }
	        });
	}
	
	public static void main(String[] args) {
		sendSync();
	}

}
